<?php
	class Hinhanh_model extends CI_Model 
	{
		function LayHinhAnh($maks) 
		{
        	$query = $this->db->get_where('HinhAnh', array('KhachSan' => $maks));
	        if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function GetARecord($maha) 
		{
        	$query = $this->db->get_where('HinhAnh', array('MaHA' => $maha));
	        if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function Xoa($maha) 
		{
        	$image = $this->GetARecord($maha);
	        if ($image) 
			{
            	foreach ($image as $row) 
				{
                	$big = $row->HinhLon;
	                $small = $row->HinhNho;

    	            if (unlink($big) && unlink($small))
        	            return $this->db->delete('HinhAnh', array('MaHA' => $maha));
            	}
	        }
    	}

	    function DoUpload($maks) 
		{
        	$this->load->library('upload');
	        $this->load->library('image_lib');

    	    if ((isset($_FILES["images"]["size"][0]) && $_FILES["images"]["size"][0] > 0)) 
			{
            	for ($i = 0; $i < count($_FILES['images']['name']); $i++) 
				{
                	$_FILES['image']['name'] = $_FILES['images']['name'][$i];
	                $_FILES['image']['type'] = $_FILES['images']['type'][$i];
    	            $_FILES['image']['tmp_name'] = $_FILES['images']['tmp_name'][$i];
        	        $_FILES['image']['error'] = $_FILES['images']['error'][$i];
            	    $_FILES['image']['size'] = $_FILES['images']['size'][$i];

                	$path_big = realpath(APPPATH . "../images/big");

	                $config = array(
				                    'upload_path' => $path_big,
                				    'allowed_types' => 'gif|jpg|png'
					                );

	                $this->upload->initialize($config);
	
    	            if ($this->upload->do_upload('image')) 
					{
	                    $file = $this->upload->data();

    	                $name = $file['file_name'];
        	            $url = $file['full_path'];

            	        $path_small = realpath(APPPATH . "../images/small");
                	    $path_thumnb = realpath(APPPATH . "../images/thumb");

	                    $config['image_library'] = 'gd2';
    	                $config['source_image'] = $url;
        	            $config['new_image'] = $path_small;
            	        $config['maintain_ratio'] = TRUE;
                	    $config['width'] = 150;
                    	$config['height'] = 100;
	                    $this->image_lib->initialize($config);
    	                $this->image_lib->resize();
        	            $this->image_lib->clear();

            	        $big_image = "images/big/" . $name;
                	    $small_image = "images/small/" . $name;

	                    $data = array('HinhLon' => $big_image, 'HinhNho' => $small_image, 'KhachSan' => $maks);
    	                $result = $this->Insert($data);
        	            if (!$result)
	                        return -1;
    	            }
        	        else 
					{
                    	echo $this->upload->display_errors();
		                return -1;
        	        }
            	}
	        }
    	    return 1;
	    }

    	function Insert($data) 
		{
    	    return $this->db->insert('HinhAnh', $data);
	    }
	}
?>